package ro.jproject.buttons.imc;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.JButton;

import ro.jproject.imc.StudentRegistrationGUI;

public class SaveButtonAction extends StudentRegistrationGUI {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TextField textFieldFirstName;
	private TextField textFieldLastName;
	private JButton save;
	private File file;

	public void setSaveButtonAction() {

	
		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				String saveFirstName = "First Name: "
						+ textFieldFirstName.getText();
				String saveLastName = " Last Name: "
						+ textFieldLastName.getText();
				file = new File("d:/studentRegistration.txt");

				try {
					RandomAccessFile raf = new RandomAccessFile(file, "rw");
					raf.writeBytes(saveFirstName);
					raf.writeBytes(saveLastName);
					raf.close();

					if (!file.exists()) {
						file.createNewFile();
					}

					FileWriter fw = new FileWriter(file.getAbsoluteFile());
					BufferedWriter bw = new BufferedWriter(fw);
					bw.write(saveFirstName);
					bw.write(saveLastName);
					bw.close();
				} catch (IOException e) {

					e.printStackTrace();
				}

				System.out.println("Registration done");
				// try {
				// showRegistration();
				// } catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				// s}
			}
		});

	}
}
